package com.cxf.windex.biz;

import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.base.constant.UserConstant;
import com.cxf.windex.base.util.IdUtil;
import com.cxf.windex.entity.BMeterials;
import com.cxf.windex.entity.BShop;
import com.cxf.windex.mapper.BMeterialsMapper;
import com.github.pagehelper.PageHelper;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @program: business_needs
 * @description: 素材Biz
 * @author: chenxf
 * @create: 2018-10-16 15:08
 **/
@Service
public class BMeterialsBiz extends BaseBiz<BMeterialsMapper,BMeterials> {
  /**
   * 素材列表
   *
   * @param limit 数量
   * @param offset 页码
   * @param orderStr  排序(升序或者降序)
   * @param shopId 店铺ID
   * @param  type 素材类型
   * @return
   */
  public List<BMeterials> list(int limit, int offset,  String orderStr,String type,Long shopId) {
    Example example = new Example(BMeterials.class);
    Example.Criteria criteria = example.createCriteria();
    if(StringUtils.isNotBlank(type)){
      criteria.andEqualTo("type",  type );
    }
    criteria.andEqualTo("shopId",  shopId );
    example.setOrderByClause("crt_time " + orderStr);
    PageHelper.startPage(offset, limit);
    return selectByExample(example);
  }

  /**
   * 保存素材
   * @param entity
   */
  public String save(BMeterials entity){
    if(entity.getId()==null|| StringUtils.isBlank(entity.getId())){
      entity.setId(UserConstant.METERIALS_ID_PREX+new IdUtil(5,1).nextId());
      insertSelective(entity);
    }else{
      updateSelectiveById(entity);
    }
    return  entity.getId();
  }
}
